//============================================================================
// Author      : Ting Lei, tinglei.geog@gmail.com
// Version     :
// Copyright   : all rights reserved
// Description : common utility functions for input/output data with postgres database server
//============================================================================

#include <postgres.h>
#include <executor/spi.h>
#include <utils/builtins.h>

#include <string.h>

//PG_MODULE_MAGIC is required only once for each library extension
//#include <postgres.h>
#include <fmgr.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

extern char *dup_pgtext(text *what);
extern char *dup_cstring(const char *what);

//copy a TEXT type into a cstring in PostgreSQL server
char *dup_pgtext(text *what)
{
    size_t len = VARSIZE(what)-VARHDRSZ;
    char *dup = palloc(len+1);
    memcpy(dup, VARDATA(what), len);
    dup[len] = 0;
    return dup;
}

//copy a cstring into a cstring in PostgreSQL server
char *dup_cstring(const char *what)
{
    size_t len = strlen(what);
    char *dup = palloc(len+1);
    memcpy(dup, what, len);
    dup[len] = 0;
    return dup;
}

